﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoyoEden.Web;
using CoyoEden.Web.Controls;
using CoyoEden.Web.Data;
using CoyoEden.Models;
using Vivasky.Core.Infrastructure;

namespace CoyoEden.Web.Controller
{
	public class AlbumController:ControllerBase
	{
		public static Album Update(List<NameValuePair<string, string>> Params, out FeedbackData msg)
		{
			msg = new FeedbackData();
			try
			{
				//need validate user?
				var user = Params.Find(item => item.name == "itemUser").value;
				var isUpdate = false;

				var id = Convert.ToInt32(Params.Find(item => item.name == "itemId").value);
				var viewPath = Params.Find(item => item.name == "AlbumViewPath").value;
				Album tempObj = null;
				if (id <= 0)
				{
					tempObj = new Album(true);
				}
				else
				{
					isUpdate = true;
					tempObj = Album.Albums.Find(c => c.Id == id);
				}
				tempObj.Title = Params.Find(item => item.name == "itemTitle").value;
				tempObj.Description = Params.Find(item => item.name == "itemDescription").value;
				//Author profile
				SimpleAuthorProfile profile = new SimpleAuthorProfile();
				profile.Ip = "";
				profile.Email = "";
				profile.Name = user;
				profile.NickName = profile.Name;
				profile.Website = "";
				tempObj.AuthorProfile = profile;

				//todo:Album's tags.

				tempObj.Save(out msg);

				//render a updated album view
				ViewManager<AlbumViewBase> view = new ViewManager<AlbumViewBase>(viewPath);
				view.Control.TheAlbum = tempObj;
				view.Control.ShowAdminLinks = GetCurrentUser().IsInRole("administrators");
				msg.Context.Add("albumHtml",view.Render());
				msg.Context.Add("IsAdd",!isUpdate);


				return tempObj;
			}
			catch (Exception ex)
			{
				ResolveException(ex, out msg);
				return null;
			}
		}
		public static void Delete(Dictionary<string, string> Params, out FeedbackData msg)
		{
			msg = new FeedbackData();
			try
			{
				var key = Params["id"];
				//delete Album
				Album item = Album.GetAlbum(key);
				item.Delete();
				item.Save();
			}
			catch (Exception ex)
			{
				ResolveException(ex, out msg);
			}
		}

		public static void SelectPics(Dictionary<string, string> Params, out FeedbackData msg)
		{
			msg = new FeedbackData();
			var currentPage = Convert.ToInt32(Params["CPage"]);
			var rowPerPage = Convert.ToInt32(Params["RPPage"]);
			var albumID = Params["AlbumID"];
			//Do page
			var album = Album.GetAlbum(albumID);
			SimplePager pager = new SimplePager();
			List<PortalFile> pics = null;
			if (album.Pics.Count > 0)
			{
				pager.PagedList<PortalFile>(album.Pics, rowPerPage, currentPage, out pics);
				//对照片排序
				pics.OrderBy(item => item.Name);
				//pics.OrderByDescending(item => item.Name);

				//Load Page view
				ViewManager<SimplePagerViewBase> viewObj = new ViewManager<SimplePagerViewBase>("~/Views/SimplePager.ascx");
				viewObj.Control.Pager = pager;
				msg.Context.Add("pagerhtml", viewObj.Render());
				msg.Context.Add("pager", pager);
			}
			else {
				msg.Context.Add("pagerhtml","");
				msg.Context.Add("pager",null);
			}
			
			//Load PicList view
			ViewManager<AlbumViewBase> albumView = new ViewManager<AlbumViewBase>("~/Views/AlbumPicsView.ascx");
			albumView.Control.TheAlbum = album;
			albumView.Control.Pics = pics;
			albumView.Control.PicHeight = 80;
			albumView.Control.PicWidth = 80;
			msg.Context.Add("piclisthtml",albumView.Render());

			msg.Context.Add("pics",pics);
		}
		public static void UploadPic(Dictionary<string, string> Params, out FeedbackData msg)
		{ 
			var imgSrc=Params["ImgSrc"];
			var albumID = Params["AlbumID"];
			imgSrc = Utils.ConvertToRelative(imgSrc, true);
			PortalFile file = new PortalFile();
			file.Name = imgSrc.Substring(imgSrc.IndexOf("picture="));
			file.Url = imgSrc;
			file.Hits = 0;
			file.Description = "No description on this file";
			file.Save(out msg);
			file.MarkOld();

			//insert into AlbumFile
			var album = Album.GetAlbum(albumID);
			album.AddPic(file);

		}
		public static void ClearPics(Dictionary<string,string> Params,out FeedbackData msg) {
			msg = new FeedbackData();
			var albumID=Params["AlbumID"];
			try
			{
				var album = Album.GetAlbum(albumID);
				album.ClearPics();
			}
			catch (Exception ex)  {
				ResolveException(ex, out msg);
			}
		}
	}
}
